package com.mall.service;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mall.dao.mapper.CommClassMapper;
import com.mall.model.CommClass;
import com.mall.model.CommClassExample;

@Service
public class CommClassService {
	@Autowired
	private CommClassMapper commClassMapper;

	public CommClassMapper getCommClassMapper() {
		return commClassMapper;
	}

	public void setCommClassMapper(CommClassMapper commClassMapper) {
		this.commClassMapper = commClassMapper;
	}

	/**
	 * 添加类别
	 * 
	 * @dagewang 2017年9月4日
	 */
	public int add(CommClass cc) throws Exception {
		cc.setCdate(new Date());
		int insert = commClassMapper.insert(cc);
		return insert;
	}

	/**
	 * 添加类别,返回id
	 * 
	 * @dagewang 2017年9月4日
	 */
	public int addload(CommClass cc) throws Exception {
		cc.setCdate(new Date());
		commClassMapper.insert(cc);
		CommClassExample ex = new CommClassExample();
		ex.createCriteria().andCommidEqualTo(cc.getCommid());
		ex.setOrderByClause("cdate desc");
		List<CommClass> selectByExample = commClassMapper.selectByExample(ex);
		if (selectByExample.size() != 0) {
			return selectByExample.get(0).getId();
		} else {
			return 0;
		}
	}

	/**
	 * 根据商品id获取分类信息
	 *
	 * @dagewang 2017年9月5日
	 */
	public List<CommClass> getByCommId(Integer id) throws Exception {
		CommClassExample ex = new CommClassExample();
		ex.createCriteria().andCommidEqualTo(id);
		List<CommClass> selectByExample = commClassMapper.selectByExample(ex);
		return selectByExample;
	}

	public CommClass getCommById(Integer id) {
		return commClassMapper.selectByPrimaryKey(id);
	}

	/**
	 * 跟新分类信息
	 *
	 * @dagewang 2017年9月8日
	 */
	public int update(CommClass comm) {
		comm.setUdate(new Date());
		return commClassMapper.updateByPrimaryKeySelective(comm);
	}

	/**
	 * 根据商品id删除分类套餐信息
	 *
	 * @dagewang 2017年9月6日
	 */
	public void delete(Integer id) {
		CommClassExample example = new CommClassExample();
		example.createCriteria().andCommidEqualTo(id);
		commClassMapper.deleteByExample(example);
	}
	/**
	 * 根据套餐id删除分类套餐信息
	 *
	 * @dagewang 2017年9月6日
	 */
	public Integer deletebyid(Integer id) throws Exception{
		return commClassMapper.deleteByPrimaryKey(id);
	}

	/**
	 * 根据商品id取类信息
	 *
	 * @dagewang 2017年9月8日
	 */
	public List<CommClass> list(Integer commid) throws Exception {
		CommClassExample ex = new CommClassExample();
		ex.createCriteria().andCommidEqualTo(commid);
		List<CommClass> selectByExample = commClassMapper.selectByExample(ex);
		return selectByExample;
	}

}
